<?php
/**
 * 商户首页
 * @author Administrator
 *
 */
class index extends CControllerMerchantBase{
	
	public function show()
	{
		$this->_title=$this->_shopName;
		$this->render("index");
	}
	
	public function first_login()
	{
		$model = new shopModel();
		
		$shopInfo = $model->selectById($this->_shopID,'del_flag=0','first_login,mail,user_name');
		
		if(!$shopInfo)
		{
			$this->sysMsg("商户不存在",1);
		}
		
		if($shopInfo["first_login"]==0)
		{
			$this->redirectUrl("/merchant/index/show");
		}
		
		$rule = CCustomFormValidator::firstLoginValidator();
		$this->formValidator->init(array("formID" => "firstLoginForm", "alertErrorMsg" => false));
		$this->formValidator->setRules($rule);
		
		if ($_POST) {
			$error = $this->formValidator->run();
			if ($error) {
				echo "error";
				exit;
			}
			else {
				//$email = $this->getRequestData("txt_email");
				$user_pwd = $this->getRequestData("txt_user_pwd");
				$pay_pwd = $this->getRequestData("txt_pay_pwd");
				
				$user_pwd = CCommon::pwdMd5($user_pwd);
				$pay_pwd = CCommon::pwdMd5($pay_pwd);
				
				$shopInfo["shop_id"] = $this->_shopID;
				//$shopInfo["mail"] = $email;
				$shopInfo["user_pwd"] = $user_pwd;
				$shopInfo["pay_pwd"] = $pay_pwd;
				$shopInfo["first_login"] = 0;
				
				$r = $model->updateByPkId($shopInfo);
				if($r)
				{
					$this->setJsBlock("alert('修改成功,点击确定进入后台');window.location.href='/merchant/index/show';");
				}
				else{
					$this->setJsBlock("alert('修改失败,请重试');");
				}
			}
		}
		$this->render("first_login",array("mail"=>$shopInfo["mail"],"userName"=>$shopInfo["user_name"]),false);
	}
	
	public function changePwd()
	{
		$this->_title="登录密码修改";
		$rule = CCustomFormValidator::changePwdValidator();
		$this->formValidator->init(array("formID" => "pwdForm", "alertErrorMsg" => false));
		$this->formValidator->setRules($rule);
		
		if ($_POST) {
			$error = $this->formValidator->run();
			if ($error) {
				echo "error";
				exit;
			}
			else {
				$old_pwd = $this->getRequestData("txt_old_pwd");
				$user_pwd = $this->getRequestData("txt_user_pwd");
				
				$old_pwd = CCommon::pwdMd5($old_pwd);
				$user_pwd = CCommon::pwdMd5($user_pwd);
				
				$shopInfo["shop_id"] = $this->_shopID;
				$shopInfo["user_pwd"] = $user_pwd;
				$shopModel = new shopModel();
				
				if($shopModel->checkUserPwd($this->_shopID,$old_pwd))
				{
					$r = $shopModel->updateByPkId($shopInfo);
					if($r)
					{
						$this->setJsBlock("alert('修改成功');window.location.href='/merchant/index/show';");
					}
					else{
						$this->setJsBlock("alert('修改失败,请重试');");
					}
				}
				else{
					$this->setJsBlock("alert('原密码错误!')");
				}
			}
		}
		$this->render("change_pwd",array("msg"=>""));
	}
	
	public function changePayPwd()
	{
		$this->_title="账户密码设置";
		$rule = CCustomFormValidator::changePayPwdValidator();
		$this->formValidator->init(array("formID" => "pwdForm", "alertErrorMsg" => false));
		$this->formValidator->setRules($rule);
	
		if ($_POST) {
			$error = $this->formValidator->run();
			if ($error) {
				echo "error";
				exit;
			}
			else {
				$old_pwd = $this->getRequestData("txt_old_pwd");
				$user_pwd = $this->getRequestData("txt_user_pwd");
	
				$old_pwd = CCommon::pwdMd5($old_pwd);
				$user_pwd = CCommon::pwdMd5($user_pwd);
	
				$shopInfo["shop_id"] = $this->_shopID;
				$shopInfo["pay_pwd"] = $user_pwd;
				$shopModel = new shopModel();
	
				if($shopModel->checkPayPwd($this->_shopID,$old_pwd))
				{
					$r = $shopModel->updateByPkId($shopInfo);
					if($r)
					{
						$this->setJsBlock("alert('修改成功');window.location.href='/merchant/index/show';");
					}
					else{
						$this->setJsBlock("alert('修改失败,请重试');");
					}
				}
				else{
					$this->setJsBlock("alert('原密码错误!')");
				}
			}
		}
		$this->render("change_pay_pwd",array("msg"=>""));
	}
	
	public function account_edit()
	{
		$shopModel = new shopModel();
	
		$this->_title="注册信息维护";
		$rule = CCustomFormValidator::shopEditValidator();
		$this->formValidator->init(array("formID"=>"editForm", "alertErrorMsg" => false));
		$this->formValidator->setRules($rule);
		
		if ($_POST) {
			$error = $this->formValidator->run();
			if ($error) {
				echo "error";
				exit;
			}
			
			$modelInfo["shop_id"] = $this->_shopID;
			$modelInfo["phone"] = $this->getRequestData("txt_phone");
			//$modelInfo["back_account"] = $this->getRequestData("txt_back_account");
			$modelInfo["contact"] = $this->getRequestData("txt_contact");
			$modelInfo["mail"] = $this->getRequestData("txt_mail");
			$modelInfo["qq"] = $this->getRequestData("txt_qq");
			
			$shopModel->updateByPkId($modelInfo);
			
			$this->setJsBlock("alert('保存成功')");
		}
		
		$shopInfo = $shopModel->selectById($this->_shopID,"del_flag=0 and shop_status=1");
		
		$shopCInfo = $shopModel->getShopCommunity($this->_shopID);
		
		
		$this->render("account_edit",array("shopInfo"=>$shopInfo, "shopCInfo"=>$shopCInfo));
	}
}